/* ========================================
 *
 * Copyright Nano IC, 2021-02-25
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *aaqqqxqfdswccdcdcdcdcdcdcdcdcd
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF Nano IC.
 *
 * ========================================
*/

#include "one_wire_bus_app.h"
#include "one_wire_bus_ext.h"
#include "app_init.h"
#include "app_keyboard_server.h"
/**
 * @brief :单总线各命令系列号
 */
OneWireSerialNumber one_wire_app_serial;

uint8_t kb_ack_flag=0;
uint8_t kb_ack_no_flag=0;
uint8_t rec_ack_flag=0;

uint16_t pad_serial=0xffff;
uint16_t kb_serial=0;

extern uint8_t one_wire_bus_phy_send_buf[ONE_WIRE_PHY_FRAME_SEND_MAX_LENGTH];

/**
 * @brief :键盘控制相关数据
 */
OneWireSetParaItemData one_wire_pata_item;

uint8_t one_wire_temp_ack_buf[ONE_WIRE_BUS_TEMP_ACK_BUF_LEN];
uint8_t one_wire_temp_ack_len;

/**
 * @brief :单总线应用层协议初始化
 * 
 * @param bool  TRUE:上电初始化   FALSE: 休眠、唤醒初始化
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_init(void)
{
    memset((uint8_t *)&one_wire_app_serial.mouse_ser,0,sizeof(OneWireSerialNumber));
    one_wire_app_serial.para_set_ser = 0xff;
    one_wire_app_serial.status_read_ser = 0xff;
    one_wire_app_serial.encry_set_ser = 0xff;
    one_wire_app_serial.upgrade_send_ser = 0xff;
    one_wire_app_serial.iic_class_out_ser = 0xff;
    one_wire_app_serial.iic_class_in_ser = 0xff;
    one_wire_app_serial.custom_in_ser = 0xff;
    one_wire_app_serial.sys_debug_ser = 0xff;
    one_wire_app_serial.manufactory_ser = 0xff;
    one_wire_app_serial.sync_packet_ser = 0xff;
    memset((uint8_t *)&one_wire_pata_item.firmware_recover,0,sizeof(OneWireSetParaItemData));
    one_wire_bus_tra_init();
    return true;
}

/**
 * @brief 1. 串口连续23uS(2 Byte对应时间)未收到数据，表示一帧接收完成，调用一次本函数。
 * @brief 2. 由于单总线协议规定了串口数据传输间隔，若芯片硬件不支持23uS串口超时中断，可用
 * @brief    定时器控制串口接收超时，可以超时1mS来实现。
 * @brief 3. 串口会接收到对方数据，也会接收到自已发送的数据，可能是一个包，也可能是多包接连，
 * @brief    串口接收不用关心具体数据，只关心接收超时，超时后调用本函数。
 * @brief 4. 串口接收缓冲最大长度250 Byte
 * 
 * @param p_uart_data ：串口接收数据指针
 * @param rec_len ： 串口接收到的数据长度
 * 
 * @return 
 */
void one_wire_bus_app_rec_callback(uint8_t *p_uart_data,uint16_t rec_len)
{ 
    one_wire_bus_tra_rec_callback(p_uart_data,rec_len);
}

/**
 * @brief 单总线运行时钟基准，系统需要每0.5毫秒调用一次本函数
 * 
 */
void one_wire_bus_app_timer_tick_callback_0ms5(void)
{
    one_wire_bus_tra_timer_tick_process_0_5ms();
}

/**
 * @brief 单总线运行时钟基准，系统需要每0.5毫秒调用一次本函数
 * 
 */
void one_wire_bus_app_timer_tick_callback_500ms(void)
{
   
	bool heart_send_f = one_wire_bus_phy_get_send_status();
	if(heart_send_f)
	{
		if( handshake>1)
		{
			one_wrie_bus_app_send_heart_beat(0);
		}
	}

}


/**
 * @brief 打包键盘数据，并发送
 * 
 * @param ctrl_key 控制键,包括CTRL ALT SHIFT
 * @param p_key 按键值存放指针
 * @param key_num 一次要传输的按键数量 
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_send_keyboard(uint8_t ctrl_key, uint8_t *p_key,uint8_t key_num)
{
//    uint8_t keyboard_data[ONE_WIRE_APP_KEYBOARD_DATA_LENGTH];
       uint8_t i;
    bool ret;
	SourcePacketStr keyboard_data;
	flagbody flag;
	keyboard_data.sid = Keyboard_Service;
	keyboard_data.cid = 0x03;
    for (i = 0; i < 8; i++)
    {
        keyboard_data.data[i] = 0;
    }

	keyboard_data.data[0] = HID_REPORT_ID_KEYBOARD_INPUT;
    keyboard_data.data[1] = ctrl_key;
    keyboard_data.data[2] = 0x00;
    if(key_num > 6)
    {
        key_num = 6;
    }
    for (int i = 0; i < 6; i++)
    {
        keyboard_data.data[i+3] = p_key[i];
    }
	if(ctrl_key == 0 && key_num == 0)
	{
		flag.needRenoteAck = 1;
	}else {
		flag.needRenoteAck = 0;
	}
	flag.protocolVersion=0;
//	flag.needRenoteAck=0;
	flag.isRTisg=0;
	flag.isAckmsg=0;
	flag.sendSource=1;
	flag.reserved=0;
	
    ret = one_wire_bus_tra_write_short_frame_to_queue((uint8_t*)&keyboard_data,
                                        ONE_WIRE_APP_KEYBOARD_DATA_LENGTH+3,
                                        &flag,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
    return ret;
}

/**
 * @brief 打包多媒体按键数据，并发送
 * 
 * @param key_one 多媒体键值0
 * @param key_two 多媒体键值1
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_send_media_key(uint16_t key_one, uint16_t key_two)
{
//    uint8_t media_data[4];

    bool ret;
	SourcePacketStr media_data;
	flagbody flag;
	media_data.sid = Keyboard_Service;
	media_data.cid = 0x03;


	media_data.data[0] = HID_REPORT_ID_CONSUME_CONTROL_INPUT;
    media_data.data[1] = (uint8_t)(key_one);;
    media_data.data[2] = (uint8_t)(key_one >> 8);;
	
	if(key_two == 0 && key_one == 0)
	{
		flag.needRenoteAck = 1;
	}else {
		flag.needRenoteAck = 0;
	}
	flag.protocolVersion=0;
//	flag.needRenoteAck=0;
	flag.isRTisg=0;
	flag.isAckmsg=0;
	flag.sendSource=1;
	flag.reserved=0;
	
    ret = one_wire_bus_tra_write_short_frame_to_queue((uint8_t*)&media_data,
                                        ONE_WIRE_APP_MEDIA_DATA_LENGTH+1,
                                        &flag,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
    return ret;

} 

/**
 * @brief 打包触模板数据，并发送
 * 
 * @param p_data 需要发送的触模板数据指针
 * @param finge_num 需要传输的触模点数，每点5 Byte，最多三点
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_send_touch(uint8_t *p_data, uint8_t finge_num)
{
   bool ret;
	SourcePacketStr touch_data;
	flagbody flag;
	touch_data.sid = Keyboard_Service;
	touch_data.cid = 0x04;


	touch_data.data[0] = HID_REPORT_ID_MOUSE_INPUT;
    memcpy(&touch_data.data[1],p_data,19);

	if((p_data[0]&0x03) == 0x01 || (p_data[5]&0x03) == 0x01 || (p_data[10]&0x03) == 0x01)
	{
		flag.needRenoteAck=1;
	}else {
		flag.needRenoteAck=0;
	}
	flag.protocolVersion=0;
//	flag.needRenoteAck=0;
	flag.isRTisg=0;
	flag.isAckmsg=0;
	flag.sendSource=1;
	flag.reserved=0;
	
    ret = one_wire_bus_tra_write_short_frame_to_queue((uint8_t*)&touch_data,
                                        19+3,
                                        &flag,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
    return ret;
}

/**
 * @brief 打包G-Sensor数据，并发送
 * 
 * @param p_data 需要发送的触模板数据指针
 * @param finge_num 需要传输的触模点数，每点5 Byte，最多三点
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_send_g_sensor(uint8_t* gsensor_buf)
{
//    uint8_t g_sensor_data[6];
       bool flag;
	
	SourcePacketStr g_sensor_data;
	g_sensor_data.sid = Keyboard_Service;
	g_sensor_data.cid = 0x05;
	
	g_sensor_data.data[0] = 0x01;
    g_sensor_data.data[1] = 0x1E;
	
	
	for (int i = 0; i < 30; i++) {
	  g_sensor_data.data[2 + i] = gsensor_buf[i];
    }
	
	flagbody flag_data;
	flag_data.protocolVersion=0;
	flag_data.needRenoteAck=0;
	flag_data.isRTisg=0;
	flag_data.isAckmsg=0;
	flag_data.sendSource=1;
	flag_data.reserved=0;
	
	
    flag = one_wire_bus_tra_write_short_frame_to_queue((uint8_t*)&g_sensor_data,
                                        34,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
										
    return flag;
}

/**
 * @brief 打包键盘自定义数据的上传
 * @brief 主要用作未定义数据的上传
 * 
 * @param p_data 需要发送的数据指针 最大长度32Byte
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_send_short_keyboard_custom_data(uint8_t state,uint8_t *p_data, uint8_t len)
{
	#if 1
    bool flag;

    if(len > ONE_WIRE_APP_USER_MAX_LENGTH)
    {
        return false;
    }
	
 	flagbody flag_data;	
	flag_data.protocolVersion= state&0x0F;
	flag_data.needRenoteAck= state>>4;
	flag_data.isRTisg= state>>5;
	flag_data.isAckmsg= state>>6;
	flag_data.sendSource= state>>7;
	flag_data.reserved=0;
		
    flag = one_wire_bus_tra_write_short_frame_to_queue(p_data,
                                        len,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
										
    return flag;
	#endif
}


/**
 * @brief 打包键盘自定义数据的上传
 * @brief 主要用作未定义数据的上传
 * 
 * @param p_data 需要发送的数据指针 最大长度64Byte
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_send_long_keyboard_custom_data(uint8_t state,uint8_t *p_data, uint8_t len)
{
	#if 1
    bool flag;

    if(len > ONE_WIRE_APP_USER_MAX_LENGTH)
    {
        return false;
    }
	
 	flagbody flag_data;	
	flag_data.protocolVersion= state&0x0F;
	flag_data.needRenoteAck= state>>4;
	flag_data.isRTisg= state>>5;
	flag_data.isAckmsg= state>>6;
	flag_data.sendSource= state>>7;
	flag_data.reserved=0;
		
  flag = one_wire_bus_tra_write_long_frame_to_queue_aaa(p_data,
                                        len,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
										
    return flag;
	#endif
}

/**
 * @brief 打包键盘自定义数据的上传,直接使用串口函数发送，不使用队列
 * @brief 主要用作未定义数据的上传
 * 
 * @param p_data 需要发送的数据指针 
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_send_keyboard_device_data(uint8_t *p_data, uint16_t len)
{
	
	#if 1
    bool flag;
	p_data[4]=(len-8);
	p_data[5]=(len-8)>>8;
	
	p_data[8]  = pad_serial;
	p_data[9]  = pad_serial>>8;
	
	p_data[10] = kb_serial;
	p_data[11] = kb_serial>>8;
	uint16_t crc16 = crc16_ccitt(&p_data[4],len-8);
	p_data[len-4] = crc16;    
	p_data[len-3] = crc16>>8;
	
	flag = one_wire_bus_phy_uart_send_one_packet(p_data,len);
    return flag;
	#endif	
}

/**
 * @brief 打包触摸板自定义数据的上传,直接使用串口函数发送，不使用队列
 * @brief 主要用作未定义数据的上传
 * 
 * @param p_data 需要发送的数据指针 
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_send_keyboard_touchpad_data(uint8_t *p_data, uint16_t len)
{
	len = 19+ 8 + 3;
	
	#if 1
    bool flag;
	uint8_t touchpad[38];
	touchpad[0]=0x5a;
	touchpad[1]=0x5a;
	touchpad[2]=0x5a;
	touchpad[3]=0x5a;
	touchpad[4]=(30);
	touchpad[5]=(30)>>8;
	
	if(!(p_data[0]&0x02) || !(p_data[5]&0x02) || !(p_data[10]&0x02))
	{
		touchpad[6]=0x90;
	}else {
		touchpad[6]=0x80;
	}
//	kb_serial++;
	p_data[8]  = pad_serial;
	touchpad[9]  = pad_serial>>8;	
	touchpad[10] = kb_serial;
	touchpad[11] = kb_serial>>8;
	
	touchpad[12] =0x3A;
	touchpad[13] =0x04;
	touchpad[14] =0x03;
	uint8_t j = 0;
	for(int i =	15;i < 34; i++ )
	{
		touchpad[i] = p_data[j];
		j++;
	}
	uint16_t crc16 = crc16_ccitt(&touchpad[4],len);
	touchpad[34] = crc16;    
	touchpad[35] = crc16>>8;
	
	touchpad[36]= 0x0a;
	touchpad[37]= 0x0d;
//	p_data[4]=(len-8);
//	p_data[5]=(len-8)>>8;
	
//	p_data[8]  = pad_serial;
//	p_data[10] = kb_serial;
//	uint16_t crc16 = crc16_ccitt(&p_data[4],len-8);
//	p_data[len-4] = crc16;    
//	p_data[len-3] = crc16>>8;
	
	flag = one_wire_bus_phy_uart_send_one_packet(touchpad,38);
    return flag;
	#endif	
}



/**
 * @brief 打包升级命令应答数据包
 * 
 * @param p_data 需要发送的数据指针
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_upgrade_cmd_ack(uint8_t *p_data, uint8_t len)
{
	
    bool flag;
	#if 0
    if(len > SEND_LONG_ONE_WIRE_DATA_MAX_LEN)
    {
        return false;
    } 
    else if(len > SEND_SHORT_ONE_WIRE_DATA_MAX_LEN)
    {
        flag = one_wire_bus_tra_write_long_frame_to_queue(p_data,
                                        len,
                                        ONE_WIRE_BUS_CMD_UPGRADE_DEV_ACK,
                                        one_wire_app_serial.upgrade_ack_ser,
                                        true
                                        );
    }else{
        flag = one_wire_bus_tra_write_short_frame_to_queue(p_data,
                                        len,
                                        ONE_WIRE_BUS_CMD_UPGRADE_DEV_ACK,
                                        one_wire_app_serial.upgrade_ack_ser,
                                        true
                                        );
    }
	#endif
    return flag;

}

/**
 * @brief 打包IIC类设置命令应答数据包
 * 
 * @param p_data 需要发送的数据指针
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_iic_class_out_ack(uint8_t *p_data, uint8_t len)
{
	#if 0
    bool flag;
    if(len > SEND_LONG_ONE_WIRE_DATA_MAX_LEN)
    {
        return false;
    } 
    else if(len > SEND_SHORT_ONE_WIRE_DATA_MAX_LEN)
    {
        flag = one_wire_bus_tra_write_long_frame_to_queue(p_data,
                                        len,
                                        ONE_WIRE_BUS_CMD_CLASS_OUT_DEV_ACK,
                                        one_wire_app_serial.iic_class_out_ack_ser,
                                        true
                                        );
    }else{
        flag = one_wire_bus_tra_write_short_frame_to_queue(p_data,
                                        len,
                                        ONE_WIRE_BUS_CMD_CLASS_OUT_DEV_ACK,
                                        one_wire_app_serial.iic_class_out_ack_ser,
                                        true
                                        );
    }
    return flag;
	#endif
}

/**
 * @brief 打包IIC类读取命令应答数据包
 * 
 * @param p_data 需要发送的数据指针
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_iic_class_in_ack(uint8_t *p_data, uint8_t len)
{
	#if 0
    bool flag;
    if(len > SEND_LONG_ONE_WIRE_DATA_MAX_LEN)
    {
        return false;
    } 
    else if(len > SEND_SHORT_ONE_WIRE_DATA_MAX_LEN)
    {
        flag = one_wire_bus_tra_write_long_frame_to_queue(p_data,
                                        len,
                                        ONE_WIRE_BUS_CMD_CLASS_IN_DEV_ACK,
                                        one_wire_app_serial.iic_class_in_ack_ser,
                                        true
                                        );
    }else{
        flag = one_wire_bus_tra_write_short_frame_to_queue(p_data,
                                        len,
                                        ONE_WIRE_BUS_CMD_CLASS_IN_DEV_ACK,
                                        one_wire_app_serial.iic_class_in_ack_ser,
                                        true
                                        );
    }
    return flag;
	#endif
}

/**
 * @brief 自定义命令应答数据包
 * 
 * @param p_data 需要发送的数据指针
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_custom_in_ack(uint8_t *p_data, uint8_t len)
{
	#if 1
	
	bool flag=false;

	flagbody flag_data;
	flag_data.protocolVersion=0;
	flag_data.needRenoteAck=1;
	flag_data.isRTisg=0;
	flag_data.isAckmsg=0;
	flag_data.sendSource=1;
	flag_data.reserved=0;

    if(len > SEND_LONG_ONE_WIRE_DATA_MAX_LEN)
    {
        return false;
    } 
    else if(len > SEND_SHORT_ONE_WIRE_DATA_MAX_LEN)
    {
        flag = one_wire_bus_tra_write_long_frame_to_queue_aaa(p_data,
                                        len,
                                        &flag_data,
                                        pad_serial, 
										kb_serial,
                                        true
                                        );
    }else{
        flag = one_wire_bus_tra_write_short_frame_to_queue(p_data,
                                        len,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
    }
    return flag;
	#endif
}

/**
 * @brief 调试命令应答数据包
 * 
 * @param p_data 需要发送的数据指针
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_debug_in_ack(uint8_t *p_data, uint8_t len)
{

    bool flag;
	#if 0
//	flagbody flag_data;
//	flag_data.protocolVersion=0;
//	flag_data.needRenoteAck=1;
//	flag_data.isRTisg=0;
//	flag_data.isAckmsg=0;
//	flag_data.sendSource=1;
//	flag_data.reserved=0;
    if(len > SEND_LONG_ONE_WIRE_DATA_MAX_LEN)
    {
        return false;
    } 
    else if(len > SEND_SHORT_ONE_WIRE_DATA_MAX_LEN)
    {
        flag = one_wire_bus_tra_write_long_frame_to_queue_aaa(p_data,
                                        len,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
    }else{
        flag = one_wire_bus_tra_write_short_frame_to_queue(p_data,
                                        len,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
    }
		#endif
    return flag;

}

/**
 * @brief 生产测试命令应答数据包
 * 
 * @param p_data 需要发送的数据指针
 * @param len 需要发送的数据长度
 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_manufactory_ack(uint8_t *p_data, uint8_t len)
{
	#if 1
    bool flag;
	
	flagbody flag_data;
	flag_data.protocolVersion=0;
	flag_data.needRenoteAck=0;
	flag_data.isRTisg=0;
	flag_data.isAckmsg=0;
	flag_data.sendSource=1;
	flag_data.reserved=0;
	
    if(len > SEND_LONG_ONE_WIRE_DATA_MAX_LEN)
    {
        return false;
    } 
    else if(len > SEND_SHORT_ONE_WIRE_DATA_MAX_LEN)
    {
        flag = one_wire_bus_tra_write_long_frame_to_queue_aaa(p_data,
                                        len,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
										true
                                        );
    }else{
        flag = one_wire_bus_tra_write_short_frame_to_queue(p_data,
                                        len,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
										true
                                        );
    }
    return flag;
	#endif
}

/**
 * @brief 同步应答数据包
 * 

 * @return true : 成功
 * @return false : 失败
 */
bool one_wire_bus_app_sync_package_ack(void)
{
	#if 1
    bool flag=0;
	
	uint8_t ack_data[3]={'A','C','K'};


	flagbody flag_data;
	flag_data.protocolVersion=0;
	flag_data.needRenoteAck=0;
	flag_data.isRTisg=0;
	flag_data.isAckmsg=1;
	flag_data.sendSource=1;
	flag_data.reserved=0;
	
	
    flag = one_wire_bus_tra_write_ack_frame_to_queue(ack_data,
                                        3,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
    return flag;
	#endif
}



bool one_wire_bus_app_sync_reportMap(uint8_t *p_data, uint16_t len)
{

    bool flag=0;
	if(len>400)
	{
		return false;
	}
	
//	uint8_t one_wire_bus_phy_send_buf[400];

	
	    uint16_t out_len;
    OneWirePacketStr *reportMap_packet = (OneWirePacketStr *)(&one_wire_bus_phy_send_buf[ONE_WIRE_PACKET_BUF_ADDR]);

    /*  同步字最少2Byte，一般是4Byte开始  */
    one_wire_bus_phy_send_buf[0] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
    one_wire_bus_phy_send_buf[1] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
	one_wire_bus_phy_send_buf[2] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
    one_wire_bus_phy_send_buf[3] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
	
	reportMap_packet->len = len+13;
	reportMap_packet->flag.protocolVersion=0;
	reportMap_packet->flag.needRenoteAck=1;
	reportMap_packet->flag.isRTisg=0;
	reportMap_packet->flag.isAckmsg=0;
	reportMap_packet->flag.sendSource=1;
	reportMap_packet->flag.reserved=0;
    reportMap_packet->seriallumber = pad_serial;
    reportMap_packet->remoteSeriallumber = kb_serial;
	
	reportMap_packet->convert.cover_data[0] = Keyboard_Service;
	reportMap_packet->convert.cover_data[1] = 0x01;
	reportMap_packet->convert.cover_data[2] = 0x01;
	reportMap_packet->convert.cover_data[3] = len;
	reportMap_packet->convert.cover_data[4] = len>>8;
	for(uint16_t i=0;i<len;i++)
	{
		reportMap_packet->convert.cover_data[i+5] = p_data[i];
	}
	uint16_t crc16 = crc16_ccitt(&one_wire_bus_phy_send_buf[4],reportMap_packet->len);
    reportMap_packet->convert.cover_data[len+5] = crc16;
    reportMap_packet->convert.cover_data[len+6] = crc16>>8;
    reportMap_packet->convert.cover_data[len+7] = ONE_WIRE_BUS_PACKET_START_END_FLAG;
    reportMap_packet->convert.cover_data[len+8] = ONE_WIRE_BUS_PACKET_START_END_FLAG2;
	
	out_len=reportMap_packet->len+8;
    flag = one_wire_bus_phy_uart_send_one_packet(one_wire_bus_phy_send_buf, out_len);
    return flag;
}

uint8_t one_wrie_bus_app_send_heart_beat(bool status)
{
    bool flag=0;
	
	SourcePacketStr heart_data;
	heart_data.sid = 0x14;
	heart_data.cid = 0x05;
	
	heart_data.data[0] = 0x02;
    heart_data.data[1] = 0x04;


	flagbody flag_data;
	flag_data.protocolVersion=0;
	flag_data.needRenoteAck=0;
	flag_data.isRTisg=0;
	flag_data.isAckmsg=0;
	flag_data.sendSource=1;
	flag_data.reserved=0;
	

	
	if(status)
	{
		uint8_t tmp_buf[20];
		 OneWirePacketStr *p_one_wire_packet = (OneWirePacketStr *)&tmp_buf[ONE_WIRE_PACKET_BUF_ADDR];
			  /*  同步字最少2Byte，一般是4Byte开始  */
		tmp_buf[0] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
		tmp_buf[1] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
		tmp_buf[2] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
		tmp_buf[3] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
		
		p_one_wire_packet->len = 4+8;
		memcpy(&(p_one_wire_packet->flag),&flag_data,sizeof(flagbody));
		p_one_wire_packet->seriallumber = pad_serial;
		p_one_wire_packet->remoteSeriallumber = kb_serial;
		
//		for(uint16_t i=0;i<4;i++)
//		{
//			p_one_wire_packet->convert.cover_data[i] = pdata[i];
//		}
		memcpy(p_one_wire_packet->convert.cover_data, &heart_data, 4);
		uint16_t crc16 = crc16_ccitt(&tmp_buf[4],p_one_wire_packet->len);

		p_one_wire_packet->convert.cover_data[4] = crc16;
		p_one_wire_packet->convert.cover_data[4+1] = crc16>>8;
		p_one_wire_packet->convert.cover_data[4+2] = ONE_WIRE_BUS_PACKET_START_END_FLAG;
		p_one_wire_packet->convert.cover_data[4+3] = ONE_WIRE_BUS_PACKET_START_END_FLAG2;
		
		one_wire_bus_uart_send_one_packet(tmp_buf,p_one_wire_packet->len+8);
		
	}else{
		flag = one_wire_bus_tra_write_short_frame_to_queue((uint8_t*)&heart_data,
									4,
									&flag_data,
									pad_serial,
									kb_serial,
									true
									);
	}

	if(flag)
	{
		return 1;
	}else{
		return 0;
	}

}

//uint8_t one_wrie_bus_app_send_accel_info()
//{
//	
//}


uint8_t one_wrie_bus_app_send_device_info()
{
    uint8_t flag=0;

	flagbody flag_data;
	flag_data.protocolVersion=0;
	flag_data.needRenoteAck=1;
	flag_data.isRTisg=0;
	flag_data.isAckmsg=0;
	flag_data.sendSource=1;
	flag_data.reserved=0;

	SourcePacketStr heart_data;
	heart_data.sid = Keyboard_Service;
	heart_data.cid = 0x08;

	uint8_t device_name_size =  sizeof(DEVICE_NAME_AAA);
	heart_data.data[0] = 0x01;
    heart_data.data[1] = device_name_size;
    memcpy(&heart_data.data[2],(uint8_t*)DEVICE_NAME_AAA,device_name_size);

	heart_data.data[2+device_name_size] = 0x02;
    heart_data.data[3+device_name_size] = 0x02;
    memcpy(&heart_data.data[4+device_name_size],(uint8_t*)&my_PnPtrs[2],2);
	heart_data.data[6+device_name_size] = 0x03;
    heart_data.data[7+device_name_size] = 0x02;
    memcpy(&heart_data.data[8+device_name_size],my_PnPtrs,2);


	flag = one_wire_bus_tra_write_short_frame_to_queue((uint8_t*)&heart_data,device_name_size+12,&flag_data,pad_serial,kb_serial,0);
	if(flag)
	{
		return 1;
	}else{
		return 0;
	}

}
uint8_t one_wrie_bus_app_send_device_extend_info()
{
    uint8_t flag=0;

	flagbody flag_data;
	flag_data.protocolVersion=0;
	flag_data.needRenoteAck=1;
	flag_data.isRTisg=0;
	flag_data.isAckmsg=0;
	flag_data.sendSource=1;
	flag_data.reserved=0;

	SourcePacketStr heart_data;
	heart_data.sid = Keyboard_Service;
	heart_data.cid = 0x0E;

	uint8_t device_name_size =  sizeof(DEVICE_NAME_AAA);
	heart_data.data[0] = 0x01;
    heart_data.data[1] = device_name_size;
    memcpy(&heart_data.data[2],(uint8_t*)DEVICE_NAME_AAA,device_name_size);

	heart_data.data[2+device_name_size] = 0x02;
    heart_data.data[3+device_name_size] = 0x02;
    memcpy(&heart_data.data[4+device_name_size],(uint8_t*)&my_PnPtrs[2],2);
	heart_data.data[6+device_name_size] = 0x03;
    heart_data.data[7+device_name_size] = 0x02;
    memcpy(&heart_data.data[8+device_name_size],my_PnPtrs,2);


	flag = one_wire_bus_tra_write_long_frame_to_queue_aaa((uint8_t*)&heart_data,device_name_size+12,&flag_data,pad_serial,kb_serial,0);
	if(flag)
	{
		return 1;
	}else{
		return 0;
	}

}
uint8_t one_wrie_bus_app_send_version_info()
{
    uint8_t flag=0;

	flagbody flag_data;
	flag_data.protocolVersion=0;
	flag_data.needRenoteAck=1;
	flag_data.isRTisg=0;
	flag_data.isAckmsg=0;
	flag_data.sendSource=1;
	flag_data.reserved=0;

	
	uint8_t device_version_size=sizeof(device_version);
	uint8_t otaPackageName_size=sizeof(otaPackageName);
	SourcePacketStr heart_data;
	heart_data.sid = Device_Management_Service;
	heart_data.cid = 0x07;

	heart_data.data[0] = 0x03;
    heart_data.data[1] = device_version_size;
    memcpy(&heart_data.data[2],(uint8_t*)device_version,device_version_size);

	heart_data.data[device_version_size+2] = 0x07;

    heart_data.data[device_version_size+3] = version_vel_size;
    memcpy(&heart_data.data[device_version_size+4],version_vel,version_vel_size);
	heart_data.data[4+device_version_size+version_vel_size] = 0x09;
    heart_data.data[5+device_version_size+version_vel_size] = 0x10;
    memcpy(&heart_data.data[6+device_version_size+version_vel_size],sn_val,16);
	heart_data.data[22+device_version_size+version_vel_size] = 0x0f;
    heart_data.data[23+device_version_size+version_vel_size] = otaPackageName_size;
    memcpy(&heart_data.data[24+device_version_size+version_vel_size],otaPackageName,otaPackageName_size);

//	flag = one_wire_bus_tra_write_short_frame_to_queue((uint8_t*)&heart_data,
//														28+otaPackageName_size+version_vel_size,
//														&flag_data,
//														pad_serial,
//														kb_serial,
//														0);
														
	if(26+device_version_size+otaPackageName_size+version_vel_size > SEND_LONG_ONE_WIRE_DATA_MAX_LEN)
    {
        return false;
    } 
    else if(26+device_version_size+otaPackageName_size+version_vel_size > SEND_SHORT_ONE_WIRE_DATA_MAX_LEN)
    {
        flag = one_wire_bus_tra_write_long_frame_to_queue_aaa((uint8_t*)&heart_data,
                                        26+device_version_size+otaPackageName_size+version_vel_size,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
    }else{
        flag = one_wire_bus_tra_write_short_frame_to_queue((uint8_t*)&heart_data,
                                        26+device_version_size+otaPackageName_size+version_vel_size,
                                        &flag_data,
                                        pad_serial,
										kb_serial,
                                        true
                                        );
    }
	if(flag)
	{
		return 1;
	}else{
		return 0;
	}

}

uint8_t one_wrie_bus_app_awaken_cmd()
{
	   bool flag=0;
	
	SourcePacketStr heart_data;
	heart_data.sid = 0x3A;
	heart_data.cid = 0x10;
	
	heart_data.data[0] = 0x01;
    heart_data.data[1] = 0x01;
	heart_data.data[2] = 0x01;

	flagbody flag_data;
	flag_data.protocolVersion=0;
	flag_data.needRenoteAck=0;
	flag_data.isRTisg=0;
	flag_data.isAckmsg=0;
	flag_data.sendSource=1;
	flag_data.reserved=0;
	
	uint8_t tmp_buf[21];
	 OneWirePacketStr *p_one_wire_packet = (OneWirePacketStr *)&tmp_buf[ONE_WIRE_PACKET_BUF_ADDR];
		  /*  同步字最少2Byte，一般是4Byte开始  */
	tmp_buf[0] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
	tmp_buf[1] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
	tmp_buf[2] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
	tmp_buf[3] = ONE_WIRE_BUS_PACKET_SYNC_WORD;
	
	p_one_wire_packet->len = 5+8;
	memcpy(&(p_one_wire_packet->flag),&flag_data,sizeof(flagbody));
	p_one_wire_packet->seriallumber = pad_serial;
	p_one_wire_packet->remoteSeriallumber = kb_serial;
	
//		for(uint16_t i=0;i<4;i++)
//		{
//			p_one_wire_packet->convert.cover_data[i] = pdata[i];
//		}
	memcpy(p_one_wire_packet->convert.cover_data, &heart_data, 5);
	uint16_t crc16 = crc16_ccitt(&tmp_buf[4],p_one_wire_packet->len);

	p_one_wire_packet->convert.cover_data[5] = crc16;
	p_one_wire_packet->convert.cover_data[5+1] = crc16>>8;
	p_one_wire_packet->convert.cover_data[5+2] = ONE_WIRE_BUS_PACKET_START_END_FLAG;
	p_one_wire_packet->convert.cover_data[5+3] = ONE_WIRE_BUS_PACKET_START_END_FLAG2;
	
	one_wire_bus_uart_send_one_packet(tmp_buf,p_one_wire_packet->len+8);
		
//	flag = one_wire_bus_tra_write_short_frame_to_queue((uint8_t*)&heart_data,
//								5,
//								&flag_data,
//								pad_serial,
//								kb_serial,
//								true
//								);
	kb_serial++;

	if(flag)
	{
		return 1;
	}else{
		return 0;
	}
	
	
}

//-------------------------------------------------------------------------------------------
/**
 * @brief 单总线收到一帧数据，进行处理
 * 
 * @param p_rec_packet 收到数据包指针
 * 
 * @return true : 成功
 * @return false : 失败
 */

uint16_t n=0;
bool one_wrie_bus_app_rec_frame_callback(OneWirePacketStr *p_rec_packet)
{
	OneWirePacketStr rec_pack_temp;
			memcpy((uint8_t*)&rec_pack_temp,p_rec_packet,sizeof(OneWirePacketStr));
	#if 1
	if(!(rec_pack_temp.flag.sendSource))
	{
		
		pad_serial=rec_pack_temp.seriallumber;
		one_wire_bus_app_sleep_or_wakeup_flag=0;
		app_sleep_timer_clear();
		flash_log_flag=0;
		flash_log_tiner_tick=0;
		
		if((rec_pack_temp.flag.needRenoteAck) )
		{  //键盘发送ack
			
			one_wire_bus_app_sync_package_ack();
			send_ack_flag=true;

		}
		if((rec_pack_temp.flag.isAckmsg) )// 平板回复ack
		{ 
			if((rec_pack_temp.convert.cover_data[0]=='a')&&(rec_pack_temp.convert.cover_data[1]=='c')&&(rec_pack_temp.convert.cover_data[2]=='k'))
			{

				if(kb_ack_flag)
				{
					kb_ack_flag = 0;
				}
				rec_ack_flag=1;
//				if(device_info_send_flag==2)  //发送完设备描述信息后发一次抬起收到ack后发送hid描述符   && handshake==2
//				{
//
//					device_info_send_flag=3;
//				}else if(device_info_send_flag==4){
//					device_info_send_flag = 5;
//					global_key_power_on_flag=1;
//				}
//				 
//				if(device_version_info_send_flag==2) //设备版本信息发送
//				{
//					device_version_info_send_flag=3;
//					
//				}
//				if (device_extend_info_send_flag==2 && accel_info_send_flag==0)   //设备扩展信息发送100ms后发送accel信息
//				{
//					device_extend_info_send_flag=3;
//					syn_100us_timer_tick=0;
//					accel_info_send_flag=1;
//				}
			}	

		}
		else if(!(rec_pack_temp.flag.sendSource))// 平板命令包
		{ 
			SourcePacketStr *p_sec_packet = (SourcePacketStr*)rec_pack_temp.convert.cover_data;
			switch(p_sec_packet->sid)
			{
				case Device_Management_Service:        // 读取键盘参数
					one_wrie_bus_app_rec_status_read(p_sec_packet);
				break;
				case OTA_Service:     					// OTA升级
					otaWrite_honor(p_sec_packet,rec_pack_temp.len-8);
				break;        
				case Maintenance_Serice:       			// LOG
					log_pro(p_sec_packet);
				break;
				case Keyboard_Service:     				// 键盘设置
					one_wrie_bus_app_rec_parameter_set(p_sec_packet);
				break;
				case Factory_Service: 
					one_wrie_bus_app_rec_factory_cmd(p_sec_packet,rec_pack_temp.len-8);
				break;
				default:
				break;
			}
			
		}
	}

//	printf("end\n");
	#endif
    return true;
}





/* [] END OF FILE */
